/* Bla */
program Ble;

	global
		//GP Keys
		__A = _control;
		__B = _alt;
		__X = _x;
		__Y = _z;
		__SELECT = _space;
		__START = _enter;
		__R = _tab;
		__L = _backspace;
	    __VOL_MINUS = _a;
		__VOL_PLUS = _s;
		__CLICK = _c;
	    
		int g1;
		int g2;

		int f1;
		int f2;
		
		int p1;
		int e1;
		
	const
		RIGHT = 0;
		LEFT = 1;
		JUMP_HEIGHT = 40;
	end
		
	begin
		//initialize screen:
		set_title("Seiti Yamashiro");
		full_screen = false;
		//graph_mode = MODE_16BITS;
		set_mode(M320x240);

		f1 = load_fpg("nukenin.fpg");

		put(f1, 19, 160, 120);
		put(f1, 2, 40, 30);
		put(f1, 3, 100, 35);

		p1 = player(300, 200);
		
		e1 = enemy_ninja(10, 200, RIGHT);
		pauser();
		
//		scroll#, fileID, graphID, bggraphID, region, flags
		start_scroll(0, f1, 20, 0, 0, 1);
		start_scroll(1, f1, 21, 0, 0, 1);
		start_scroll(2, f1, 22, 0, 0, 1);
		start_scroll(3, f1, 23, 0, 0, 1);

//		scroll[1].ratio = 200;
//		scroll[1].ratio = 300;
//		scroll[2].ratio = 400;
//		scroll[3].ratio = 500;								

		loop
			if (key(__VOL_PLUS) && key(__VOL_MINUS))
				let_me_alone();
				exit("",0);
			end
			frame;
		end
		
	end

process player(int x, int y);
	
	private 
		int flag = 0; 
		int indice = 0;
		int direction = 0;
		
		int idle[] = (5, 6, 7);
		int idlecount = 3;
		int idleindice = 0;
		
		int run[] = (8, 9, 10, 11, 12, 10);
		int runcount = 6;
		int runindice = 0;
	
		int jump[] = (13, 14);		
		int jumpcount = 2;
		int jumpindice = 0;
		int jumpheight = JUMP_HEIGHT;
	
		int jumping = 0;
	
	begin
		file = f1;
		graph = 13;

		loop
			// Setting the sprite direction
			if (direction == LEFT)
				flags = 0;
			else
				flags = 1;				
			end	
		
			// Moving the sprite
			if (key(_right) || key(_left))
				if (!jumping)
					if (key(_right))
						direction = RIGHT;
						x += 2;
					elseif  (key(_left))				
						direction = LEFT;
						x -= 2;							
					end								
					movebg(direction);
					if (flag > 2)
						graph = run[++runindice % runcount];
						flag = 0;
					end
				else
					if (key(_right))
						direction = RIGHT;
						x += 1;
					elseif  (key(_left))				
						direction = LEFT;
						x -= 1;							
					end								
					movebg(direction);
				end
			end
						
			// Jumping			
			if (key(__A) && !jumping)
				jumping = 1;
			end
			
			if (jumping)
				if (jumpheight > 0)
					jumpheight -= 5;
					y -= 5;
					if 	(JUMP_HEIGHT - jumpheight > 20)				
						jumpindice = 0;
					else
						jumpindice = 1;
					end										
				elseif (jumpheight > -JUMP_HEIGHT)
					jumpheight -= 5; //todo: colocar algum chão
					y += 5;
					jumpindice = 1;
				else
					jumpheight = JUMP_HEIGHT;
					jumping = 0;
				end	
				if (flag > 2)
					graph = jump[jumpindice]; 
					flag = 0;
				end
			end

						
			// Making it fly			
			if (key(_up))
				y -= 2;
			elseif (key(_down))
				y += 2;
			end
			
			// Zooming
			if (key(__X))
				size_x += 4;		
				size_y += 4;
			elseif (key(__Y))
				size_x -= 4;		
				size_y -= 4;
			end

			// Rotating
			if (key(__R))
				angle += 5000;
			elseif (key(__L))
				angle -= 5000;
			end

			if (flag > 2 && !jumping && !key(_right) && !key(_left))
				runindice = 0;
				graph = idle[++idleindice % idlecount];
				flag = 0;
			end

			flag++;
			frame;
		end
	end

process enemy_ninja(int x, int y, int direction);
	
	private 
		int flag = 0; 
		int indice = 0;
		
		int idle[] = (5, 6, 7);
		int idlecount = 3;
		int idleindice = 0;
		
		int run[] = (8, 9, 10, 11, 12, 10);		
		int runcount = 6;
		int runindice = 0;
	
		int p_id;
		
	begin
		file = f1;
		graph = 13;

		loop
			if(p_id = collision(type player))
				if (direction == LEFT)
					direction = RIGHT;
					x += 4;
				else
					direction = LEFT;
					x -= 4;
				end	
			end

			if (direction == LEFT)
				flags = 0;
			else
				flags = 1;				
			end	

			if (direction == RIGHT)
				x += 1;
			elseif  (direction == LEFT)
				x -= 1;
			end								

			if (flag > 2)
				graph = run[++runindice % runcount];
				flag = 0;
			end

			flag++;
			frame;
		end
	end

function movebg(int direction);
	begin
		
		if  (direction == LEFT)		
			scroll[0].x0 -= 1;
			scroll[1].x0 -= 2;
			scroll[2].x0 -= 3;
			scroll[3].x0 -= 4;
		elseif (direction == RIGHT)
			scroll[0].x0 += 1;
			scroll[1].x0 += 2;
			scroll[2].x0 += 3;
			scroll[3].x0 += 4;		
		end
		return 0;
	end
	
process pauser();
	private
		st_pressed = 0; 
		done = 0; 
		freezed = 0;  
		int text;
	begin
		loop
			if (key(__START)) st_pressed=1; end;

			if (!key(__START) && st_pressed == 1)
				//this process remembers if the game is paused or not:
				//if freezed==0 every process is running and gets freezed here
				if (freezed == 0 && done == 0)
					signal((type player), s_freeze); //process 1 = a process from your game
					//        signal((type process2),s_freeze); //another process from your game
					freezed = 1; 
					done = 1;
					text = write(0, 290, 20, 4, "pause");
				end;

				//if freezed==1 every process is freezed and gets woken up here
				if(freezed==1 and done==0)
					signal((type player), s_wakeup); //same as above
//					signal((type process2),s_wakeup);
//					...
					freezed=0; 
					done=1;
					delete_text(text);
				end;
				done=0; 
				st_pressed=0;
			end;
 		frame;
		end;
	end;
	

